import type { ServiceAPIURI, ServiceResult } from "../api/api.config.service";

type CountryConfig = ServiceResult<ServiceAPIURI.ConfCoun>;
type OfficialConfig = ServiceResult<ServiceAPIURI.ConfOther>;

export default class Config {
	private static coun_code: string;
	private static lang_code: string;
	private static country_conf: CountryConfig;
	private static official_conf: OfficialConfig;

	/** 当前的国家代码 */
	public static get country(){
		return Config.coun_code;
	}
	/** 当前的语言代码 */
	public static get language(){
		return Config.lang_code;
	}
	/** 当前的国家配置 */
	public static get countryConf(){
		return Config.country_conf;
	}
	/** 当前的商业化配置 */
	public static get officialConf(){
		return Config.official_conf;
	}

	public static get taskRemove(){
		return Config.official_conf.taskRemove;
	}

	public static get taskRound(){
		return Config.official_conf.taskRound;
	}

	public static setCountryConfig(conf: CountryConfig){
		Config.coun_code = conf.country;
		Config.lang_code = conf.language;
		Config.country_conf = conf;
	}

	public static setOfficialConfig(conf: OfficialConfig){
		Config.official_conf = conf;
	}

	public static safeCountry(country: string){
		if(typeof country != 'string'){
			country = country_code_list[country];
		}
		if(!country_code_list.includes(country)){
			country = country_code_list[20]; // 默认国家代码
		}
		return country;
	}

	/**
	 * 格式化货币
	 * @param symbol
	 */
	public static symbolString(symbol: number){
		let deco = ['.', ',', ' '];
		symbol = mtec.number.fixedNum(symbol, Config.countryConf.round, true);
		let [str, DPart] = symbol.toString().split('.');

		if(Config.CountryIs(10) && str.length>3){
			str = [str.slice(0, str.length-3).replace(/\B(?=(\d{2})+(?!\d))/g, deco[Config.countryConf.thousand-1]), str.slice(str.length-3)].join(deco[Config.countryConf.thousand-1]);
		}else{
			str = str.replace(/\B(?=(\d{3})+(?!\d))/g, deco[Config.countryConf.thousand-1]);
		}

		if(DPart){
			str = [str, DPart].join(deco[Config.countryConf.point-1]);
		}

		if(Config.countryConf.front==1){
			str = [Config.countryConf.symbol, str].join(' ');
		}else{
			str = [str, Config.countryConf.symbol].join(' ');
		}

		return str;
	}

	public static CountryIs(code: number){
		return Config.country == country_code_list[code];
	}
}

// Country code list
// AR: 0
// AU: 1
// BR: 2
// CA: 3
// CL: 4
// CO: 5
// DE: 6
// FR: 7
// GB: 8
// ID: 9
// IN: 10
// JP: 11
// KR: 12
// KZ: 13
// MX: 14
// NG: 15
// NZ: 16
// RU: 17
// TH: 18
// TR: 19
// US: 20
// VN: 21
// ZA: 22
/** 有效国家代码列表 */
const country_code_list = [ "QVI=", "QVU=", "QlI=", "Q0E=", "Q0w=", "Q08=", "REU=", "RlI=", "R0I=", "SUQ=", "SU4=", "SlA=", "S1I=", "S1o=", "TVg=", "Tkc=", "Tlo=", "UlU=", "VEg=", "VFI=", "VVM=", "Vk4=", "WkE=" ].map(el=>el.decodeAtoB);
